using System;
using System.Data;
using System.Data.SqlClient;

using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataAccess;
using Sunyi.Project.WebPoint.DataContract;

using ActivitySystem.DataContract;

namespace ActivitySystem.DataAccess
{
    public partial class ActivityDao : IDao<Activity>
    {
        public Activity Select(Guid id)
        {
            Activity activity = null;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Activity_Select";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        activity = CreateEntityByReader(reader);
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return activity;
        }

        public void Insert(Activity activity)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Activity_Insert";
            command.Parameters.Add(new SqlParameter("@Id", activity.Id));
            command.Parameters.Add(new SqlParameter("@Title", activity.Title));
            command.Parameters.Add(new SqlParameter("@LauncherId", activity.LauncherId));
            command.Parameters.Add(new SqlParameter("@IsPublic", activity.IsPublic));
            command.Parameters.Add(new SqlParameter("@IsApproved", activity.IsApproved));
            command.Parameters.Add(new SqlParameter("@StartTime", activity.StartTime));
            command.Parameters.Add(new SqlParameter("@EndTime", activity.EndTime));
            command.Parameters.Add(new SqlParameter("@Description", activity.Description));
            command.Parameters.Add(new SqlParameter("@IsActive", activity.IsActive));
            command.Parameters.Add(new SqlParameter("@InsertTimeStamp", activity.InsertTimeStamp));
            command.Parameters.Add(new SqlParameter("@UpdateTimeStamp", activity.UpdateTimeStamp));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Update(Activity activity)
        {
            if (activity.UpdateTimeStamp == DateTime.MinValue) activity.UpdateTimeStamp = DateTime.Now;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Activity_Update";
            command.Parameters.Add(new SqlParameter("@Id", activity.Id));
            command.Parameters.Add(new SqlParameter("@Title", activity.Title));
            command.Parameters.Add(new SqlParameter("@LauncherId", activity.LauncherId));
            command.Parameters.Add(new SqlParameter("@IsPublic", activity.IsPublic));
            command.Parameters.Add(new SqlParameter("@IsApproved", activity.IsApproved));
            command.Parameters.Add(new SqlParameter("@StartTime", activity.StartTime));
            command.Parameters.Add(new SqlParameter("@EndTime", activity.EndTime));
            command.Parameters.Add(new SqlParameter("@Description", activity.Description));
            command.Parameters.Add(new SqlParameter("@IsActive", activity.IsActive));
            command.Parameters.Add(new SqlParameter("@InsertTimeStamp", activity.InsertTimeStamp));
            command.Parameters.Add(new SqlParameter("@UpdateTimeStamp", activity.UpdateTimeStamp));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Delete(Guid id)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Activity_Delete";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        #region Private Method

        internal static Activity CreateEntityByReader(IDataReader reader)
        {
            Activity activity = new Activity();
            if (reader["Id"] != DBNull.Value) activity.Id = (Guid)reader["Id"];
            if (reader["Title"] != DBNull.Value) activity.Title = reader["Title"] as string;
            if (reader["LauncherId"] != DBNull.Value) activity.LauncherId = (Guid)reader["LauncherId"];
            if (reader["IsPublic"] != DBNull.Value) activity.IsPublic = (Boolean)reader["IsPublic"];
            if (reader["IsApproved"] != DBNull.Value) activity.IsApproved = (Boolean)reader["IsApproved"];
            if (reader["StartTime"] != DBNull.Value) activity.StartTime = (DateTime)reader["StartTime"];
            if (reader["EndTime"] != DBNull.Value) activity.EndTime = (DateTime)reader["EndTime"];
            if (reader["Description"] != DBNull.Value) activity.Description = reader["Description"] as string;
            if (reader["IsActive"] != DBNull.Value) activity.IsActive = (Boolean)reader["IsActive"];
            if (reader["InsertTimeStamp"] != DBNull.Value) activity.InsertTimeStamp = (DateTime)reader["InsertTimeStamp"];
            if (reader["UpdateTimeStamp"] != DBNull.Value) activity.UpdateTimeStamp = (DateTime)reader["UpdateTimeStamp"];

            return activity;
        }

        #endregion
    }
}